
package com.shop.cereshop.admin.service.wallet.impl;

import com.shop.cereshop.admin.dao.wallet.BuyerWalletDAO;
import com.shop.cereshop.admin.service.history.impl.CereWebHistoryServiceImpl;
import com.shop.cereshop.admin.service.wallet.BuyerWalletService;
import com.shop.cereshop.commons.domain.user.CerePlatformUser;
import com.shop.cereshop.commons.utils.PayPasswordUtils;
import com.shop.cereshop.commons.utils.TimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 业务实现类
 * 用户钱包表
 * </p>
 *
 * @author wang.yao
 * @date 2025-08-26
 */
@Slf4j
@Service
public class BuyerWalletServiceImpl implements BuyerWalletService {
    @Resource
    private BuyerWalletDAO buyerWalletDAO;

    @Autowired
    private CereWebHistoryServiceImpl cereWebHistoryService;

    @Override
    public void adminModifyPayPassword(Long buyerUserId, String newPayPassword, HttpServletRequest request) {
         CerePlatformUser user  =(CerePlatformUser)request.getAttribute("user");
        String action="";
        String result ="";
        if (!PayPasswordUtils.isValidPayPassword(newPayPassword)) {
            throw new IllegalArgumentException("支付密码格式错误，应为6位数字");
        }
        String encrypted = PayPasswordUtils.encryptPayPassword(newPayPassword);
        String updateTime = TimeUtils.yyMMddHHmmss();
        int number = buyerWalletDAO.adminUpdatePayPasswordAndResetError(buyerUserId, encrypted, true, updateTime);
        if (number > 0){
            result="成功";
        }else {
            result="失败";
        }
        action=user.getName()+"修改了"+"用户id为"+buyerUserId+"的用户支付密码";
        cereWebHistoryService.addHistory(user.getPlatformUserId(),user.getName(),user.getPhone(),user.getAvatar()==null ?null:user.getAvatar(),action,result,1);

        log.info("管理员修改用户支付密码成功 buyerUserId={} ", buyerUserId);
    }
}
